package com.jhd.fuzhou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.GetImageByUrl;
import com.jhd.fuzhou.util.OpenWps;
import com.jhd.fuzhou.vo.AdministrativeAccessoryVo;
import com.jhd.fuzhou.vo.SuperviseEnclosureVo;

import java.util.List;

public  class TaskDocumentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SuperviseEnclosureVo> superviseEnclosureVoList;private Context context;

    public TaskDocumentAdapter(List<SuperviseEnclosureVo> superviseEnclosureVoList, Context context) {
        this.superviseEnclosureVoList = superviseEnclosureVoList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = getViewHolderByViewType(parent,viewType);
        return viewHolder;
    }


    private RecyclerView.ViewHolder getViewHolderByViewType(ViewGroup parent,int viewType) {
        View itemPic=LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false);
        View itemHtml=LayoutInflater.from(context).inflate(R.layout.item_pdf, parent, false);
        RecyclerView.ViewHolder holder=null;
        switch (viewType){
            case 0:
                holder=new TaskDocumentAdapter.ViewHolderPic(itemPic);
                break;
            case 1:
                holder=new TaskDocumentAdapter.ViewHolderHtml(itemHtml);
                break;
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        //获取最后一个.的位置
        int lastIndexOf = superviseEnclosureVoList.get(position).getEnclosureUrl().lastIndexOf(".");
        //获取文件的后缀名 .jpg
        String suffix = superviseEnclosureVoList.get(position).getEnclosureUrl().substring(lastIndexOf);
        if(suffix.equals(".png")||suffix.equals(".jpg")||suffix.equals(".jpeg")){
            return 0;
        }else{
            return 1;
        }
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==0){
            ((TaskDocumentAdapter.ViewHolderPic)holder).tv_name.setText(superviseEnclosureVoList.get(position).getEnclosureName());
            GetImageByUrl getImageByUrl = new GetImageByUrl();
            ((ViewHolderPic)holder).iv_is_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(((ViewHolderPic)holder).image.getVisibility()==View.VISIBLE){
                        ((ViewHolderPic)holder).image.setVisibility(View.GONE);
                        ((ViewHolderPic)holder).iv_is_show.setImageResource(R.mipmap.ic_down);
                    }else{
                        ((ViewHolderPic)holder).image.setVisibility(View.VISIBLE);
                        getImageByUrl.setImage(((TaskDocumentAdapter.ViewHolderPic)holder).image, superviseEnclosureVoList.get(position).getEnclosureUrl());
                        ((ViewHolderPic)holder).iv_is_show.setImageResource(R.mipmap.ic_go);
                    }
                }
            });
        }
        if(getItemViewType(position)==1){
            ((TaskDocumentAdapter.ViewHolderHtml)holder).tv_title.setText(superviseEnclosureVoList.get(position).getEnclosureName());
            WebSettings webSettings = ((TaskDocumentAdapter.ViewHolderHtml)holder).webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
            //缩放开关，设置此属性，仅支持双击缩放，不支持触摸缩放
            webSettings.setSupportZoom(true);
            //设置是否可缩放，会出现缩放工具（若为true则上面的设值也默认为true）
            webSettings.setBuiltInZoomControls(true);
            ((TaskDocumentAdapter.ViewHolderHtml) holder).webView.setInitialScale(300);
            int lastIndexOf = superviseEnclosureVoList.get(position).getEnclosureUrl().lastIndexOf(".");
            //获取文件的后缀名 .jpg
            String suffix = superviseEnclosureVoList.get(position).getEnclosureUrl().substring(lastIndexOf);
            if(suffix.equals(".pdf")){
                ((TaskDocumentAdapter.ViewHolderHtml)holder).webView.loadUrl("file:///android_asset/index.html?" + superviseEnclosureVoList.get(position).getEnclosureUrl());
            }else if(suffix.equals(".docx")||suffix.equals(".doc")){
                //((ShowAdapter.ViewHolderHtml)holder).webView.loadUrl("http://docs.google.com/gview?embedded=true&url="+administrativeAccessoryVoList.get(position).getFileUrl());
            }
            ((ViewHolderHtml)holder).iv_is_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(((ViewHolderHtml)holder).webView.getVisibility()==View.VISIBLE){
                        ((ViewHolderHtml)holder).webView.setVisibility(View.GONE);
                        ((ViewHolderHtml)holder).iv_is_show.setImageResource(R.mipmap.ic_down);
                    }else{
                        if(!suffix.equals(".png")&&!suffix.equals(".jpg")&&!suffix.equals(".jpeg")&&!suffix.equals(".pdf")){
                            if(suffix.equals(".doc")||suffix.equals(".docx")||suffix.equals(".txt")||suffix.equals(".xlsx")||suffix.equals(".xls")||suffix.equals(".rtf")||suffix.equals(".pptx")||suffix.equals(".ppt")){
                                if(OpenWps.checkWps(context)){
                                    OpenWps.openWps(context,superviseEnclosureVoList.get(position).getEnclosureUrl());
                                }else{
                                    CustomToast.showText(context,"请先安装wps!", 3000,0,0).show();
                                }
                            }else{
                                CustomToast.showText(context,"暂不支持此类型文件打开!", 3000,0,0).show();
                            }
                        }else{
                            ((ViewHolderHtml)holder).webView.setVisibility(View.VISIBLE);
                            ((ViewHolderHtml)holder).iv_is_show.setImageResource(R.mipmap.ic_go);
                        }
                    }
                }
            });


        }
    }

    @Override
    public int getItemCount() {
        return superviseEnclosureVoList.size();
    }

    class ViewHolderHtml extends RecyclerView.ViewHolder {
        TextView tv_title;/*PDFView pdfView;*/
        WebView webView;
        ImageView iv_is_show;
        public ViewHolderHtml(View itemView) {
            super(itemView);
            this.tv_title = itemView.findViewById(R.id.tv_title);
            this.webView = itemView.findViewById(R.id.webView);
            iv_is_show = (ImageView) itemView.findViewById(R.id.iv_is_show);
        }
    }
    class ViewHolderPic extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView image,iv_is_show;
        LinearLayout linearLayout;
        public ViewHolderPic(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            image = (ImageView) itemView.findViewById(R.id.image);
            iv_is_show = (ImageView) itemView.findViewById(R.id.iv_is_show);
        }
    }
}

